package cn.yyd.org.service.impl;

import cn.yyd.basic.service.impl.BaseServiceImpl;
import cn.yyd.basic.utils.MD5Utils;
import cn.yyd.basic.utils.StrUtils;
import cn.yyd.org.domain.Employee;
import cn.yyd.org.mapper.EmployeeMapper;
import cn.yyd.org.service.IEmployeeService;
import cn.yyd.user.domain.Logininfo;
import cn.yyd.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
* @Description: 部门业务层实现类
* @Author: Mr.She
* @Version: 1.0
* @Date:  2022/6/21 16:42
*/
@Service
// 只读事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void save(Employee employee) {
        // 1.初始化emplpyee，并保存，这是引用传递
        initEmployee(employee);
        employeeMapper.save(employee);
        // 2.初始化logininfo，并保存
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        // 3.将logininfo的主键Id设置到employee的外键，并修改
        employee.setLogininfoId(logininfo.getId());
    }

    /** 
    * @Title: initEmployee
    * @Description: 初始化employee
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/7/6 16:43  
    * @Parameters: [employee]
    * @Return void
    */
    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setSalt(salt);
        employee.setPassword(saltPassword);
    }

    /** 
    * @Title: employee2Logininfo
    * @Description: 将employee的数据拷贝到logininfo中
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/7/6 16:44  
    * @Parameters: [employee]
    * @Return cn.itsource.user.domain.Logininfo
    */
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    

    @Override
    public void update(Employee employee) {
        super.update(employee);
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }
}
